package org.apogee.mil.client.main.pages;

import org.apogee.mil.client.factories.ServerServiceFactory;
import org.apogee.mil.client.main.MainForm;
import org.apogee.mil.client.utils.CDCUtils;
import org.apogee.mil.client.utils.Session;
import org.apogee.mil.shared.User;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.googlecode.gwt.crypto.client.TripleDesCipher;

public class LoginForm extends LayoutContainer {

	private FormData formData;
	private TripleDesCipher cipher;
	private int sessionId = -1;
	private MainForm portal = null;
	private User currentUser = null;

	public LoginForm(MainForm adminForm) {
		this.sessionId = Session.getSessionId();
		this.portal = adminForm;

		cipher = new TripleDesCipher();
		cipher.setKey(CDCUtils.SECRET_KEY.getBytes());
	}

	@Override  
	protected void onRender(Element parent, int index) {  
		super.onRender(parent, index);
		createForm1();  
	}  

	private void createForm1() { 
		
		FormPanel simple = new FormPanel(); 
		simple.setHeading("Login");  
		simple.setFrame(true);  
		simple.setWidth(350);
		simple.setLabelWidth(100);
		

		final TextField<String> firstName = new TextField<String>();  
		firstName.setFieldLabel("User Name");  
		firstName.setAllowBlank(false);  
		firstName.getFocusSupport().setPreviousId(simple.getButtonBar().getId());  
		firstName.setAutoWidth(true);
		simple.add(firstName, formData);  

		final TextField<String> passwd = new TextField<String>();  
		passwd.setFieldLabel("Password");  
		passwd.setAllowBlank(false);  
		passwd.setPassword(true);
		passwd.setAutoWidth(true);
		simple.add(passwd, formData); 

		Button b = new Button("Login");  
		b.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {

				String user = firstName.getValue().trim();
				String pass = passwd.getValue().trim();

				try {
					pass = cipher.encrypt(pass);
				} catch (Exception e) {
					MessageBox.alert("Error", "Unable to Login", null);
					return;
				}
				ServerServiceFactory.getLoginService(sessionId).login(user, pass, new AsyncCallback<User>() {

					@Override
					public void onSuccess(User result) {
						if (result == null) {
							MessageBox.alert("Error", "Wrong Username/Password. Try Again.", null);
							return;
						} else {
							Info.display("Success", "Successfully Logged in " + result.getUserName());
						}
						currentUser = result;
						currentUser.setSessionId(sessionId);
						
						ServerServiceFactory.initializeFactory(sessionId, currentUser.getAccessLevel());
						ServerServiceFactory.addUser(Session.getSessionId(), currentUser);
						
						if (portal != null) {
							portal.enableFrames(currentUser);
						}		
					}

					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert("Error", "Unable to Login at this time\n" + caught.getMessage(), null);
					}
				});

			}
		});
		simple.addButton(b);  

		simple.setButtonAlign(HorizontalAlignment.CENTER);

		FormButtonBinding binding = new FormButtonBinding(simple);  
		binding.addButton(b);  
		add(simple); 

	}
}
